import ajax from './ajax'
// import Password from 'antd/lib/input/Password'
import jsonp from 'jsonp'

//登录
export const reqLogin=(userName,passWord)=>ajax('/api/user/login',{userName,passWord},'POST');

//添加用户
export const reqAddUser=(user)=>ajax('/api/addUser',user,'POST');

// 获取一级或某个二级分类列表
export const reqCategorys = (parentId) => ajax('/api/category/list', {parentId},'POST')

//添加分类
export const reqAddCategory = (parentId, categoryName,parentName) => ajax('/api/category/add',
    { "parentId":parentId, "name":categoryName,parentName }, 'POST')


//更新品类名称
export const reqUpdateCategory = ({categoryId, categoryName}) => ajax('/api/category/update',
    { "id":categoryId, "name":categoryName }, 'POST')


// 根据分类 ID 获取分类
// export const reqCategory = (categoryId) => ajax('/manage/category/info', {categoryId})
// 获取一级或某个二级分类列表
export const reqCategory = (id) => ajax('/api/category/list', {id},'POST')

// 获取商品分页列表
export const reqProducts = (pageNum, pageSize) => ajax('/api/product/list', {pageNum, pageSize})
// 根据 ID/Name 搜索产品分页列表
export const reqSearchProducts = ({pageNum, pageSize, searchType, searchName}) => ajax('/api/product/list', { pageNum, pageSize, [searchType]: searchName, })
// 添加更新商品
export const reqAddOrUpdateProduct = (product) => ajax('/manage/product/' + (product._id ? 'update' : 'add'), product, 'post')
//上下架商品
export const reqUpdateProductStatus = (productId, status) => ajax('/manage/product/updateStatus', { productId, status }, 'POST')
//删除图片
export const reqDeleteImg = (name) => ajax('/manage/img/delete', {name}, 'post')

// 添加角色
export const reqAddRole = (roleName) => ajax('/manage/role/add', {roleName}, 'POST')


// 获取角色列表
export const reqRoles = () => ajax('/manage/role/list')


// 更新角色(给角色设置权限)
export const reqUpdateRole = (role) => ajax('/manage/role/update', role, 'POST')

// 添加/更新用户
export const reqAddOrUpdateUser = (user) => ajax('/manage/user/'+(user._id ? 'update' : 'add'), user, 'POST')

// 获取用户列表
export const reqUsers = () => ajax('/manage/user/list')


// 删除用户
export const reqDeleteUser = (userId) => ajax('/manage/user/delete', {userId}, 'POST')

/*
通过 jsonp 请求获取天气信息
*/
export function reqWeather(city) {
    const url =
        `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p4
9MVra6urFRGOT9s8UBWr2`
    return new Promise((resolve, reject) => {
        jsonp(url, {
            param: 'callback'
        }, (error, response) => {
            if (!error && response.status == 'success') {
                const {dayPictureUrl, weather} = response.results[0].weather_data[0]
                resolve({dayPictureUrl, weather})
            } else {
                alert(' 获取天气信息失败')
            }
        })
    })
}